/*
 *  This file is part of Cube.
 *
 *  Cube is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  Cube is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Cube.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

package cube.charts;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.GradientPaint;
import java.awt.Point;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.dial.DialBackground;
import org.jfree.chart.plot.dial.DialPlot;
import org.jfree.chart.plot.dial.DialPointer;
import org.jfree.chart.plot.dial.StandardDialFrame;
import org.jfree.chart.plot.dial.StandardDialScale;
import org.jfree.data.general.DefaultValueDataset;
import org.jfree.ui.GradientPaintTransformType;
import org.jfree.ui.StandardGradientPaintTransformer;

/**
 *
 * @author akermanl
 */
public class DialChart extends Chart {
    
  
    private DefaultValueDataset dataset;
    private int minValue;
    private int maxValue;
   
    public  DialChart(String title, int xPos, int yPos, int width, int height, int minValue, int maxValue) {
        super(title, xPos, yPos, width, height);    
        
        dataset = new DefaultValueDataset(0);      
        
        DialPlot plot = new DialPlot();
        //x,y,w,h
        plot.setView(0.21, 0.0, 0.58, 0.30);
        plot.setDataset(dataset);
        //setValue(0);
              
        StandardDialFrame dialFrame = new StandardDialFrame();        
        dialFrame.setRadius(0.90);
        dialFrame.setForegroundPaint(Color.darkGray);
        dialFrame.setStroke(new BasicStroke(3.0f));
        plot.setDialFrame(dialFrame);
        
        GradientPaint gp = new GradientPaint(new Point(),new Color(255, 255, 255), new Point(), new Color(240, 240, 240));
        DialBackground sdb = new DialBackground(gp);
        sdb.setGradientPaintTransformer(new StandardGradientPaintTransformer(GradientPaintTransformType.VERTICAL));
        plot.addLayer(sdb);
        
        // lowerBound,  upperBound, startAngle,  extent, majorTickIncrement, minorTickCount
        //StandardDialScale scale = new StandardDialScale(-50, 50, 125.0, -70.0, 1, 10);
        //StandardDialScale scale = new StandardDialScale(minValue, maxValue, 125.0, -70, 30, 2);
        
        int majorTickIncrement = 0;
        int minorTickCount = 0;
        
        if ((maxValue-minValue)>=100){
            majorTickIncrement = 30;
            minorTickCount = 2;
        }  else {
            majorTickIncrement = 1;
            minorTickCount = 10;
        }
        
        StandardDialScale scale = new StandardDialScale(minValue, maxValue, 125.0, -70, majorTickIncrement, minorTickCount);
        scale.setTickRadius(0.88);
        scale.setTickLabelOffset(0.07);
        //scale.setMajorTickIncrement(1.0);
        plot.addScale(0, scale);
         
        DialPointer needle = new DialPointer.Pin();
        needle.setRadius(0.82);      
        plot.addLayer(needle);
        
        JFreeChart chart1 = new JFreeChart(plot);
        //ChartFrame frame2 = new ChartFrame("First", chart1); 
       
        ChartPanel chartPanel = new ChartPanel(chart1);
        chartPanel.setPreferredSize(new java.awt.Dimension(400, 200));
        //chartPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); 
        
        //frame2.pack(); 
        //frame2.setVisible(true);
        
        super.setContentPane(chartPanel);
    }


    public void setValue(double value) {
        dataset.setValue(value);
    }
    
    public void resetValues() {
        dataset.setValue(0);
    }
    
}
